﻿//SharePoint Form Generator 
//By Alon Havivi(havivi@gmail.com, http://havivi.blogspot.com/) 
//For full source code and Terms Of Use,
//visit http://sfg.codeplex.com/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;

namespace SFG
{
    public partial class SFGDesignForm : System.Web.UI.Page
    {
        SharePointFormGenerator wp1 = null;
        protected void Page_Load(object sender, EventArgs e)
        {
           

            Label LabelTitle = (Label)FindControl("LabelTitle"); ;//new Label();            
            LabelTitle.Text = SetAudience();
        }

        private string SetAudience()
        {
            string url = Request.QueryString["url"];
            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    using (SPLimitedWebPartManager oWebpartMngr = web.GetLimitedWebPartManager(url, PersonalizationScope.Shared))
                    {
                        string id = Request.QueryString["id"];
                       //"http://havivi2003/sites/myteam/default.aspx";
                        SharePointFormGenerator wp1 = (SharePointFormGenerator)oWebpartMngr.WebParts[id];
                        //return wp1.FormTitle;
                        return "sorry, under construction... </br>please check on codeplex project site for updates</br>http://sfg.codeplex.com/";
                        //SharePointFormGenerator wp1 = GetWebPartById(web, url, id);
                        //SPContext.Current.Web.AllowUnsafeUpdates = true;
                        //SPListItemCollection items = newWeb.Lists["Pagina's"].Items;
                        //foreach (SPListItem file in items)
                        //{
                        //if (file.Name.ToLower() == "default.aspx")
                        //{
                        //Microsoft.SharePoint.Publishing.PublishingPage defaultPage = Microsoft.SharePoint.Publishing.PublishingPage.GetPublishingPage(file);
                        //defaultPage.CheckOut();
                        //oWebpartMngr.Web.AllowUnsafeUpdates = true;
                        //oWebpartMngr.Web.GetFile(file.UniqueId).CheckOut();
                        //    foreach (Microsoft.SharePoint.WebPartPages.WebPart wp in oWebpartMngr.WebParts)
                        //    {

                        //        if (wp.Title == "SharePoint Form Generator")
                        //        {
                        //            SharePointFormGenerator wp1 = (SharePointFormGenerator)wp;
                                  return wp1.SubmitButtonCaption;
                        //            //string groupName = null;
                        //            //foreach (SPGroup group in newWeb.Groups)
                        //            //{
                        //            //    if (group.Name.Contains("Eigenaar"))
                        //            //    {
                        //            //        groupName = group.Name;
                        //            //        break;
                        //            //    }
                        //            //}
                        //            //if (groupName != null)
                        //            //{
                        //            //    wp.AuthorizationFilter = ";;;;" + groupName;
                        //            //    oWebpartMngr.Web.AllowUnsafeUpdates = true;

                        //            //    oWebpartMngr.SaveChanges(wp);
                        //            //    //oWebpartMngr.Web.GetFile(file.UniqueId).CheckIn("Set Audience targeting on the Beheertaken web part");
                        //            //   // oWebpartMngr.Web.GetFile(file.UniqueId).Publish("Set Audience targeting on the Beheertaken web part");
                        //            //    oWebpartMngr.Web.AllowUnsafeUpdates = false;
                        //            //    newWeb.AllowUnsafeUpdates = false;

                        //            //    break;
                        //            //}
                        //        }

                        //        // }

                        //        // }
                        //    }
                    }
                    return "test";
                }
            }
        }
        /// <summary>   
        /// 
        /// Gets the web part by id.  
        /// </summary>   
        /// <param name="web">The web.</param>   
        /// <param name="url">The URL.</param>   
        /// <param name="id">The id.</param>   
        /// <param name="manager">The web part manager.</param>   
        /// <returns></returns>   
        //internal static SharePointFormGenerator GetWebPartById(SPWeb web, string url, string id, out SPLimitedWebPartManager manager)
        //{
        //    manager = web.GetLimitedWebPartManager(url, PersonalizationScope.Shared);
        //    WebPart wp = manager.WebParts[id];
        //    if (wp == null)
        //    {
        //        manager = web.GetLimitedWebPartManager(url, PersonalizationScope.User);
        //        wp = manager.WebParts[id];
        //    }
        //    return (SharePointFormGenerator)wp;
        //}
    }



}
